package com.zhiyou100.blog.dao.impl;

import com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetException;
import com.zhiyou100.blog.dao.EndorseDao;
import com.zhiyou100.blog.entity.Endorse;
import com.zhiyou100.blog.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-10-25 10:49
 */
public class EndorseDaoImpl implements EndorseDao {

    public List<Endorse> getListEndorseZanXuUid(int uid, boolean isAgree) {
        try (Connection conn = JDBCUtils.getConnection();) {
            QueryRunner runner = new QueryRunner();
            String sql = "";
            if (isAgree) {//赞同
                sql = "select uid,mid,zan_xu from tab_zanxu where zan_xu = 1  and uid= ? ;";
            } else {
                sql = "select uid,mid,zan_xu from tab_zanxu where zan_xu = -1  and uid=?;";
            }
            BeanListHandler<Endorse> listHandler = new BeanListHandler<Endorse>(Endorse.class);
            return runner.query(conn, sql, listHandler, uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getEndorseUserZanXuCount(int mid, int uid, boolean isAgree) {
        try (Connection conn = JDBCUtils.getConnection();) {
            QueryRunner runner = new QueryRunner();
            String sql = "";
            if (isAgree) {//赞同
                sql = "select COUNT(*) from tab_zanxu where zan_xu = 1 and mid=? and uid= ? ;";
            } else {
                sql = "select COUNT(*) from tab_zanxu where zan_xu = -1 and mid=? and uid=?;";
            }
            System.out.println(sql);
            ScalarHandler<Object> handler = new ScalarHandler<>();
            return (Long) runner.query(conn, sql, handler, mid, uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0L;
    }

    @Test
    public void test() {
        System.out.println(getEndorseUserZanXuCount(1, 4, true));
//        System.out.println(getEndorseZanXuCountByUid(4, true));
    }

    @Override
    public List<Endorse> getListEndorseZanXu(int mid, boolean isAgree) {
        try (Connection conn = JDBCUtils.getConnection();) {
            QueryRunner runner = new QueryRunner();
            String sql = "";
            if (isAgree) {//赞同
                sql = "SELECT z.`MID`,z.`uid`,z.`zan_xu` FROM tab_message m LEFT JOIN tab_zanxu z ON m.`MID`=z.`MID` WHERE m.mid=? AND z.zan_xu=1;";
            } else {
                sql = "SELECT z.`MID`,z.`uid`,z.`zan_xu` FROM tab_message m LEFT JOIN tab_zanxu z ON m.`MID`=z.`MID` WHERE m.mid=? AND z.zan_xu=-1;";
            }
            BeanListHandler<Endorse> listHandler = new BeanListHandler<Endorse>(Endorse.class);
            return runner.query(conn, sql, listHandler, mid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addEndorseZanXu(int mid, int uid, boolean isAgree) {
        try (Connection conn = JDBCUtils.getConnection();) {
            QueryRunner runner = new QueryRunner();
            String sql = "";
            if (isAgree) {//赞同
                sql = "insert into  tab_zanxu values(?,?,1);";
            } else {
                sql = "insert into  tab_zanxu values(?,?,-1);";
            }
            return runner.update(conn, sql, mid, uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }


    @Override
    public int updateEndorseZanXu(int mid, int uid, boolean isAgree) {
        try (Connection conn = JDBCUtils.getConnection();) {
            QueryRunner runner = new QueryRunner();
            String sql = "";
            if (isAgree) {//赞同
                sql = "update tab_zanxu set zan_xu = 1 where uid = ? and mid=?;";
            } else {
                sql = "update tab_zanxu set zan_xu = -1 where uid = ? and mid=?;";
            }
            ScalarHandler<Object> handler = new ScalarHandler<>();
            return runner.update(conn, sql, handler, mid, uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
